package com.lcxsoft.orderservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lcxsoft.commonutils.JwtUtils;
import com.lcxsoft.commonutils.R;
import com.lcxsoft.feign.clients.GoodsClient;
import com.lcxsoft.feign.clients.PartnerClient;
import com.lcxsoft.feign.clients.UserClient;
import com.lcxsoft.feign.entity.Goods;
import com.lcxsoft.feign.entity.PartnerUser;
import com.lcxsoft.orderservice.entity.Orders;
import com.lcxsoft.orderservice.query.OrderQuery;
import com.lcxsoft.orderservice.query.OrderSaveQuery;
import com.lcxsoft.orderservice.service.OrdersService;
import com.lcxsoft.orderservice.url.OrderServiceURL;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcx
 * @since 2022-01-22
 */
@RestController
@RequestMapping(OrderServiceURL.packageUrl)
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @Resource
    private UserClient userClient;

    @Resource
    private PartnerClient partnerClient;

    @Resource
    private GoodsClient goodsClient;

    /**
     * 创建订单
     * @param query
     * @return
     */
    @RequestMapping(value = OrderServiceURL.OrderUrl.createOrder, method = RequestMethod.POST)
    public R createOrder(@RequestBody OrderSaveQuery query){
        //保存订单
        Orders orders = new Orders();
        orders.setUserId(query.getUserId());
        orders.setGoodsId(query.getGoodsId());
        orders.setOrdersId(UUID.randomUUID().toString().replaceAll("-", "").toUpperCase());
        orders.setOrdersAmount(query.getOrdersAmount());
        orders.setOrdersPrice(query.getOrdersPrice());
        orders.setOrdersTottle(query.getOrdersTottle());
        orders.setCreatetime(LocalDateTime.now());
        orders.setSchoolId(userClient.querySchoolByUserId(query.getUserId()));
        boolean save = ordersService.save(orders);
        //给学校代理人加钱
        Goods goods = goodsClient.queryGoodsById(query.getGoodsId());
        Map<String, Object> params = new HashMap<>();
        params.put("dormitoryId", goods.getDormitoryId());
        params.put("ordersTottle", query.getOrdersTottle());
        partnerClient.addMoneyByDormitoryId(params);
        return save ? R.ok().message("保存成功") : R.error().message("保存失败");
    }

    /**
     * 获取订单列表
     * @param query
     * @return
     */
    @RequestMapping(value = OrderServiceURL.OrderUrl.queryOrders, method = RequestMethod.POST)
    public R queryOrders(OrderQuery query){
        return ordersService.findOrders(query);
    }

    /**
     * 获取今日订单数量
     * @return
     */
    @PostMapping(OrderServiceURL.OrderUrl.countTodayOrderNumber)
    public R countTodayOrderNumber(){
        return ordersService.countTodayOrderNumber();
    }

    /**
     * 获取今日销售额
     * @return
     */
    @PostMapping(OrderServiceURL.OrderUrl.countTodayOrderMoney)
    public R countTodayOrderMoney(){
        return ordersService.countTodayOrderMoney();
    }

    /**
     * 获取总订单数
     * @return
     */
    @PostMapping(OrderServiceURL.OrderUrl.countAllOrders)
    public R countAllOrders(){
        int count = ordersService.count(null);
        return R.ok().data("countOrders", count);
    }

    /**
     * 根据token获取学校订单
     * @param request
     * @return
     */
    @PostMapping(OrderServiceURL.OrderUrl.queryOrderByToken)
    public R queryOrderByToken(OrderQuery query,HttpServletRequest request){
        //查到partnerId
        String partnerId = JwtUtils.getMemberIdByJwtToken(request);
        //根据partnerId查询schoolId
        Map<String, String> params = new HashMap<>();
        params.put("partnerId", partnerId);
        PartnerUser partnerUser = partnerClient.queryPartnerById(params);
        query.setSchoolId(partnerUser.getSchoolId());
        return ordersService.findOrders(query);
    }

    /**
     * 根据token获取当日订单数
     * @param request
     * @return
     */
    @PostMapping(OrderServiceURL.OrderUrl.queryTodayOrderCountByToken)
    public R queryTodayOrderCountByToken(HttpServletRequest request){
        //查到partnerId
        String partnerId = JwtUtils.getMemberIdByJwtToken(request);
        //根据partnerId查询schoolId
        Map<String, String> params = new HashMap<>();
        params.put("partnerId", partnerId);
        PartnerUser partnerUser = partnerClient.queryPartnerById(params);
        return ordersService.queryTodayOrderCountByToken(partnerUser.getSchoolId());
    }

    /**
     * 根据Token获取当地订单额
     * @param request
     * @return
     */
    @PostMapping(OrderServiceURL.OrderUrl.queryTodayOrderMoneyByToken)
    public R queryTodayOrderMoneyByToken(HttpServletRequest request){
        //查到partnerId
        String partnerId = JwtUtils.getMemberIdByJwtToken(request);
        //根据partnerId查询schoolId
        Map<String, String> params = new HashMap<>();
        params.put("partnerId", partnerId);
        PartnerUser partnerUser = partnerClient.queryPartnerById(params);
        return ordersService.queryTodayOrderMoneyByToken(partnerUser.getSchoolId());
    }

    /**
     * 根据Token获取总订单数量
     * @param request
     * @return
     */
    @PostMapping(OrderServiceURL.OrderUrl.queryAllOrdersByToken)
    public R queryAllOrdersByToken(HttpServletRequest request){
        //查到partnerId
        String partnerId = JwtUtils.getMemberIdByJwtToken(request);
        //根据partnerId查询schoolId
        Map<String, String> params = new HashMap<>();
        params.put("partnerId", partnerId);
        PartnerUser partnerUser = partnerClient.queryPartnerById(params);
        int count = ordersService.count(new LambdaQueryWrapper<Orders>().eq(Orders::getSchoolId, partnerUser.getSchoolId()));
        return R.ok().data("countOrders", count);
    }


}

